package cn.shyouchi.wuguanjia.android.ui.train;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.train.TrainApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.CircularProgressBar;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class TrainMainActivity extends BaseActivity {


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.pro_rate)
    CircularProgressBar mProRate;
    @BindView(R.id.tv_rate)
    TextView mTvRate;
    @BindView(R.id.tv_newly_num)
    TextView mTvNewlyNum;
    @BindView(R.id.pro_rate2)
    CircularProgressBar mProRate2;
    @BindView(R.id.tv_rate2)
    TextView mTvRate2;

    TrainApi mTrainApi;
    TrainEntity mMaster;
    TrainEntity mPost;

    private boolean needRefresh = true;


    @Override
    public int initContentView() {
        return R.layout.activity_train_main;
    }

    @Override
    public void initInjector() {
        mTrainApi = new TrainApi();
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(getTitle().toString());
        setSupportActionBar(mTopBar.getToolbar());

        EventBus.getDefault().register(this);

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    public void gotoMaster(View v) {
        startActivity(new Intent(mContext, TrainMasterActivity.class)
                .putExtra("curPage", mMaster.getCurrentpage())
                .putExtra("maxPage", mMaster.getTotalpage()));
    }


    @Override
    protected void onResume() {
        if (needRefresh) {
            needRefresh = false;
            fetchTrainData();
        }
        super.onResume();
    }

    public void gotoTrain(View v) {
        startActivity(new Intent(mContext, TrainPostActivity.class)
                .putExtra("curPage", mPost.getCurrentpage())
                .putExtra("maxPage", mPost.getTotalpage()));
    }


    private void fetchTrainData() {
        mTrainApi.getTrainData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult<List<TrainEntity>>>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult<List<TrainEntity>> listBaseRequestResult) {
                        if (!listBaseRequestResult.getResultMessage().isEmpty()) {
                            fillMaster(listBaseRequestResult.getResultMessage().get(0));
                            fillPost(listBaseRequestResult.getResultMessage().get(1));
                        }
                    }
                });
    }

    private void fillMaster(TrainEntity entity) {
        mMaster = entity;
        mProRate.setProgress((int) entity.getSchedule());
        mTvRate.setText(entity.getSchedule() + "%");
    }

    private void fillPost(TrainEntity entity) {
        mPost = entity;
        mProRate2.setProgress((int) entity.getSchedule());
        mTvRate2.setText(entity.getSchedule() + "%");
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void needRefresh(DataChangeEvent event) {
        needRefresh = true;
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
